<?PHP
class EmplBO extends BusinessObject{
  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
			"id"                =>	"empl_id", 
			"login"             =>	"empl_login",
			"password"          =>	"empl_password",
			"email"             =>	"empl_email",
			"fname"			        =>	"empl_firstname",
			"lname"			        =>	"empl_lastname",
			"last_login"        =>	"empl_date_login",
			"deleted"           =>	"empl_deleted", 
			"disabled"          =>	"empl_disabled",
			"reg_date"		      =>	"empl_date_register",
			"desc"    		      =>	"empl_description"
		);

    BusinessObject::setAttributes();
  }
  /* constuctor */
  function EmplBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("empl");
    $this->setAttributes();
  }

  /*  function checkLogin  
      if username exist and not this user's then return false else true.
  */
  function checkLogin(){
    $u = new EmplBO();
    $u->login = $this->login;
	  $u->deleted = 0;
    $u->Load(0,1);
    if($u->Next()){
      if($u->id != $this->id)
        return(false);
    }
    return(true);
  }
  /*  function checkEmail  
      if email already exist and not this user's then return false else true.
  */
  function checkEmail(){
    $u = new EmplBO();
    $u->email = $this->email;
	  $u->deleted = 0;
    $u->Load(0,1);
    if($u->Next()){
      if($u->id != $this->id)
        return(false);
    }
    return(true);
  }

  /*  Save  */
  function Save($userID)
  {
    if(is_numeric($userID) && $userID<>0){
      $this->id = "$userID";
      if(!($this->checkLogin())) return (" Employee with Login '{$this->login}' already exist");
      if(!($this->checkEmail())) return (" Employee with Email '{$this->email}' already exist");
      $this->Update("{$this->fields[id]} = '$userID'");  
    } else{
      if(!($this->checkLogin())) return (" Employee with Login '{$this->login}' already exist");
      if(!($this->checkEmail())) return (" Employee with Email '{$this->email}' already exist");
      $this->Insert();
      $this->id = $this->getInsertID();
    }
  }

  function setLastLogin()
  {
    OB_START();
     $this->last_login = date("Y-m-d H:i:s");
     $this->Update($this->fields["id"]."=".$this->id);
    OB_END_CLEAN();
  }
}
?>
